﻿using System;
using System.Diagnostics;
using FsCms.Entity;
using FsCms.Service;
using FsCms.Service.DAL;

namespace TestConsole
{
    class Program
    {
        public static IFreeSql mysql = new FreeSql.FreeSqlBuilder()
          .UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3307;User ID=root;Password=abc123456;Initial Catalog=FsCms;Charset=utf8;SslMode=none;Max pool size=10")
          .UseAutoSyncStructure(true)

          .UseMonitorCommand(
              cmd =>
              {
                  Trace.WriteLine(cmd.CommandText);
              }, //监听SQL命令对象，在执行前
              (cmd, traceLog) =>
              {
                  Console.WriteLine(traceLog);
              }) //监听SQL命令对象，在执行后
          .UseLazyLoading(true)
          .Build();

        static void Main(string[] args)
        {
            //var list = mysql.Select<ArticleContent>().Where(w => w.Id > 0).ToList();
            UserMenuTest();
            UserRoleTest();

            Console.WriteLine("获取的记录条数：");
            Console.ReadKey();
        }

        static void UserMenuTest()
        {
            var select = mysql.Select<SysRoleMenu>();

            var list = select.LeftJoin<SysUserRole>((a, b) => a.RoleId == b.RoleId)
                .LeftJoin<SysMenu>((a, c) => a.MenuId == c.Id)
                .Where<SysUserRole>((rm, ur) => ur.UserId == 1);

            var sql = list.ToSql("a.Id,c.Id as MenuId, c.MenuName,c.MenuUrl,c.ParentID");
            Console.WriteLine(sql);
            var resultList = list.ToList();

            foreach (var item in resultList)
            {
                Console.WriteLine($"{item.Menu.Id}-{item.Menu.MenuName}-{item.Menu.MenuUrl}-{item.Menu.ParentID}");
            }

            var sql2 = list.ToSql();
        }

        static void UserRoleTest()
        {
            var select = mysql.Select<SysUserRole>();

            var list = select.LeftJoin<SysUser>((a, b) => a.UserId == b.Id)
                .LeftJoin<SysRole>((a, c) => a.RoleId == c.Id)
                .Where(w => w.Status == 1);

            var sql = list.ToSql("a.Id,b.UserName,c.RoleName");
            Console.WriteLine(sql);
            var resultList = list.ToList();

            foreach (var item in resultList)
            {
                Console.WriteLine($"{item.Id}-{item.Role.RoleName}-{item.User.UserName}");
            }

            var sql2 = list.ToSql();
        }
    }
}
